<?php
App::uses('Service', 'Model');
App::uses('CaseTag', 'Model');
App::uses('Tag', 'Model');
class CaseController extends AppController
{
	public $name = 'Case';


	public $paginate = array(
        'limit' => 9,
        'order' => array(
            'Case.priority' => 'desc',
            'Case.updated' => 'desc'
        ),
        'contain' => array('Case'),
    );

	public function Index()
	{
        $data = $this->getCases();
        $caseTags = $this->getCaseTags($data);

        $service = new Service();
        $serviceList = $service->find('list', array(
            'conditions' => array(
                'is_deleted' => 0
            )
        ));
        $Tag = new Tag();
        $tagList = $Tag->find('list');

        $this->set('cases', $caseTags);
        $this->set('services', $serviceList);
        $this->set('tagList', $tagList);
	}

	public function Tag($tagId = null)
	{
        if ($tagId == null) {
            $this->redirect('/');
        }

        $data = $this->getCasesByTag($tagId);
        $caseTags = $this->getCaseTags($data);

        $service = new Service();
        $serviceList = $service->find('list', array(
            'conditions' => array(
                'is_deleted' => 0
            )
        ));
        $Tag = new Tag();
        $tagList = $Tag->find('list');

        $this->set('cases', $caseTags);
        $this->set('services', $serviceList);
        $this->set('tagList', $tagList);
	}

    public function View($id = null)
    {
        $this->paginate['limit'] = 1;
        if ($id == null) {
            $this->redirect('/');
        }

        $data = $this->paginate('Case', array(
            'Case.id' => $id,
        ));
        if (!empty($data)) {

            $neighborsId = $this->_getNeighborsId($id);

            $this->set('dataDetail', $data[0]);
            $this->set('neighborsId', $neighborsId ); 
        } else {
            $this->redirect('/');
        }

    }

    private function _getNeighborsId($currId)
    {
        $caseType = $this->_getCaseType();
        $conditions = array(
            'is_deleted' => 0
        );
        if ($caseType != 0) {
            $conditions['Case.service_id'] =  $caseType;
        
        }
        $allcaseIds = $this->Case->find('list', array(
            'conditions' => $conditions,
            'order' => array(
                'priority' => 'desc',
                'updated' => 'desc'
            )
        ));

        $idIndex = array_keys($allcaseIds);

        $currIndex = array_search($currId, $idIndex);

        $prevId = array_key_exists($currIndex-1, $idIndex) ? $idIndex[$currIndex-1] : false;
        $nextId = array_key_exists($currIndex+1, $idIndex) ? $idIndex[$currIndex+1] : false;
        return array(
            'prev_id' => $prevId,
            'next_id' => $nextId
        );
    }



    private function _getCaseType()
    {
        $namedParams = $this->request->params['named']; 
        $caseType = 0;
        if (isset($namedParams['type'])
            && is_numeric($namedParams['type'])
        ) {
            $caseType = $namedParams['type'];
        }
        $this->set('caseType', $caseType);
        return $caseType;    
    }

    private function getCaseTags($data)
    {
        $CaseTag = new CaseTag();
        $result = $data;
        foreach ($data as $k => $case) {
            $res = $CaseTag->find('all', array(
                'conditions' => array(
                    'case_id' => $case['Case']['id'] 
                )
            ));
            $tags = array();
            if ($res) {
                foreach ($res as $ct) {
                    $tags[] = $ct['CaseTag']['tag_id'];
                }
            }
            $result[$k]['Case']['tags'] = $tags;
        }    
        return $result;
    }

    private function getCases()
    {
        $data = array();
        $namedParams = $this->request->params['named']; 
        $caseType = 0;
        $conditions = array(
            'Case.is_deleted' => 0
        );
        if (isset($namedParams['type'])
            && is_numeric($namedParams['type'])
        ) {
            $caseType = $namedParams['type'];
            if ($caseType != 0) {
                $conditions['Case.service_id'] =  $caseType;
            }
        }
        $data = $this->paginate('Case', $conditions);
        $this->set('caseType', $caseType);
        return $data;
    }

    private function getCasesByTag($tagId)
    {
        $CaseTag = new CaseTag();

        $res = $CaseTag->find('all', array(
            'fields' => array('case_id'),
            'conditions' => array(
                'tag_id' => $tagId 
            )
        ));
        $caseIds = Set::extract('/CaseTag/case_id', $res);

        $data = array();
        $conditions = array(
            'Case.is_deleted' => 0,
            'Case.id' => $caseIds
        );
        $data = $this->paginate('Case', $conditions);
        $this->set('caseType', 0);
        return $data;
    }
}
